package com.conan.app.dailyflickr.activity;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.conan.app.flickrdaily.R;
import com.conan.app.dailyflickr.provider.ProviderUtils;
import com.conan.app.dailyflickr.provider.ProviderUtils.DailyInfo;
import com.conan.app.dailyflickr.provider.ProviderUtils.LAYOUT_TYPE_VALUE;
import com.conan.app.dailyflickr.provider.ProviderUtils.LayoutType;

public class DailyFlickrSettings extends Activity implements OnClickListener{

    Button mBtnDone;

    RadioButton mRbUpdateLayout_0, mRbUpdateLayout_1, mRbUpdateLayout_2;
    TextView mTvUpdateLayout_0, mTvUpdateLayout_1, mTvUpdateLayout_2;
    LayoutType mLayout;
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    LAYOUT_TYPE_VALUE mLayoutSelectId;
    RadioGroup mRbLayoutGroup ;
    DailyInfo info = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        setContentView(R.layout.flickr_widget_setting_layout);

        Intent intent = getIntent();
        if(intent != null){
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        if( mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID){
            finish();
        }

        mRbUpdateLayout_0 =(RadioButton) findViewById(R.id.radiobutton_stg_layout_normal_id);
        mRbUpdateLayout_0.setOnClickListener(this);
        mTvUpdateLayout_0 =(TextView) findViewById(R.id.radiobutton_stg_layout_normal_txt);
        mTvUpdateLayout_0.setOnClickListener(this);
        mRbUpdateLayout_1 =(RadioButton) findViewById(R.id.radiobutton_stg_layout_large_id);
        mRbUpdateLayout_1.setOnClickListener(this);
        mTvUpdateLayout_1 =(TextView) findViewById(R.id.radiobutton_stg_layout_large_txt);
        mTvUpdateLayout_1.setOnClickListener(this);
        mRbUpdateLayout_2 =(RadioButton) findViewById(R.id.radiobutton_stg_layout_lanscape_id);
        mRbUpdateLayout_2.setOnClickListener(this);
        mTvUpdateLayout_2 =(TextView) findViewById(R.id.radiobutton_stg_layout_lanscape_txt);
        mTvUpdateLayout_2.setOnClickListener(this);
        mRbLayoutGroup =(RadioGroup) findViewById(R.id.radiogroup_layout_type_id);		


        info = ProviderUtils.getWidgetInfoFromProvider(getApplicationContext(), mAppWidgetId);
        mLayout = new LayoutType(getApplicationContext(), info._layout);		
        mBtnDone = (Button) findViewById(R.id.bt_flickr_setting_done_id);
        mBtnDone.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // store preferece				
                // int type = mRbLayoutGroup.getCheckedRadioButtonId();

                if(mLayoutSelectId != null && 
                        info._layout != mLayoutSelectId.ordinal()) {
                    LayoutType layoutSelect = new LayoutType(getApplicationContext(),mLayoutSelectId);

                    info._page = (info._page * layoutSelect._PER_PAGE)/ mLayout._PER_PAGE;
                    if(info._page < 1) info._page = 1;
                    info._layout = mLayoutSelectId.ordinal();
                    // update provider
                    ProviderUtils.updateWidgetInfoToProvider(getApplicationContext(), info);
                    DailyFlickrWidget.sDailyInfos.put(mAppWidgetId, info);

                    DailyFlickrWidget.makeChangeLayoutType(getApplicationContext(), mAppWidgetId);
                }
                finish();
            }
        });


        switch (LAYOUT_TYPE_VALUE.values()[info._layout]) {
        case normal:
            mRbLayoutGroup.check(R.id.radiobutton_stg_layout_normal_id);
            break;
        case large:
            mRbLayoutGroup.check(R.id.radiobutton_stg_layout_large_id);
            break;

        default:
            mRbLayoutGroup.check(R.id.radiobutton_stg_layout_normal_id);
        }



    }

    @Override
    protected void onResume() {
        super.onResume();

        overridePendingTransition(0, 0);

    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0, 0);

    }

    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.radiobutton_stg_layout_normal_id:
            mLayoutSelectId = LAYOUT_TYPE_VALUE.normal;
            break;
        case R.id.radiobutton_stg_layout_normal_txt:
            mRbUpdateLayout_0.setChecked(true);
            mLayoutSelectId = LAYOUT_TYPE_VALUE.normal;
            break;		
        case R.id.radiobutton_stg_layout_large_id:
            mLayoutSelectId = LAYOUT_TYPE_VALUE.large;
            break;
        case R.id.radiobutton_stg_layout_large_txt:
            mRbUpdateLayout_1.setChecked(true);
            mLayoutSelectId = LAYOUT_TYPE_VALUE.large;
            break;
        default:
            break;
        }


    }

}
